package io.github.talelin.latticy.controller.v1.K;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.core.annotation.LoginRequired;
import io.github.talelin.latticy.common.mybatis.Page;
import io.github.talelin.latticy.common.util.PageUtil;
import io.github.talelin.latticy.model.K.BCollectionDO;
import io.github.talelin.latticy.model.K.BCollectionDetailDO;
import io.github.talelin.latticy.service.K.BCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.DeletedVO;
import io.github.talelin.latticy.vo.PageResponseVO;
import io.github.talelin.latticy.vo.UpdatedVO;

import javax.validation.constraints.Min;
import javax.validation.constraints.Max;
import javax.validation.constraints.Positive;

import org.springframework.web.bind.annotation.RestController;

/**
* @author generator@TaleLin
* @since 2021-06-08
*/
@RestController
@RequestMapping("/v1/BCollection")
public class BCollectionController {

    @Autowired
    private BCollectionService BCollectionService;
    
    @PostMapping("")
    public CreatedVO create() {
        return new CreatedVO();
    }

    @PutMapping("/{id}")
    public UpdatedVO update(@PathVariable @Positive(message = "{id.positive}") Integer id) {
        return new UpdatedVO();
    }

    @DeleteMapping("/{id}")
    public DeletedVO delete(@PathVariable @Positive(message = "{id.positive}") Integer id) {
        return new DeletedVO();
    }

    @GetMapping("/{id}")
    public BCollectionDO get(@PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id) {
        BCollectionDO b_data = BCollectionService.getById(id);
        if (b_data == null) {
            throw new NotFoundException("b_gateway not found", 10022);
        }
        return b_data;
    }

    @GetMapping("/page")
    public PageResponseVO<BCollectionDO> page(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}")
            @Max(value = 30, message = "{page.count.max}") Integer count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Integer page,
            @RequestParam(name = "StartTime") String StartTime,
            @RequestParam(name = "EndTime") String EndTime,
            @RequestParam(name = "dwmc") String dwmc,
            @RequestParam(name = "bgateway_name") String bgateway_name,
            @RequestParam(name = "collector_name") String collector_name,
            @RequestParam(name = "collector_power") String collector_power,
            @RequestParam(name = "tresult") String tresult,
            @RequestParam(name = "hresult") String hresult,
            //垃圾
            @RequestParam(name = "dwmc1") String dwmc1,
            @RequestParam(name = "bgateway_name1") String bgateway_name1,
            @RequestParam(name = "collector_name1") String collector_name1,
            @RequestParam(name = "dwmc2") String dwmc2,
            @RequestParam(name = "bgateway_name2") String bgateway_name2,
            @RequestParam(name = "collector_name2") String collector_name2,
            @RequestParam(name = "dwmc3") String dwmc3,
            @RequestParam(name = "bgateway_name3") String bgateway_name3,
            @RequestParam(name = "collector_name3") String collector_name3,
            @RequestParam(name = "dwmc4") String dwmc4,
            @RequestParam(name = "bgateway_name4") String bgateway_name4,
            @RequestParam(name = "collector_name4") String collector_name4
    ) {
        Page<BCollectionDO> pager = new Page<>(page, count);
        QueryWrapper<BCollectionDO> wrapper = new QueryWrapper<>();
        if(StartTime != ""){
            wrapper.ge("collector_time",StartTime);
        }
        if(EndTime != ""){
            wrapper.le("collector_time",EndTime);
        }
//        if(dwmc != ""){
//            wrapper.eq("dwmc",dwmc);
//        };
//        if(bgateway_name != ""){
//            wrapper.eq("b_gateway_name",bgateway_name);
//        };
//        if(collector_name != ""){
//            wrapper.eq("collector_name",collector_name);
//        };
        if(collector_power != ""){
            wrapper.le("collector_power",collector_power);
        };
        if(tresult != ""){
            wrapper.eq("t_result",tresult);
        };
        if(hresult != ""){
            wrapper.eq("h_result",hresult);
        };

//        if(dwmc1 != ""){
//            wrapper.or().eq("dwmc",dwmc1);
//        };
//        if(bgateway_name1 != ""){
//            wrapper.or().eq("b_gateway_name",bgateway_name1);
//        };
//        if(collector_name1 != ""){
//            wrapper.or().eq("collector_name",collector_name1);
//        };
//        if(dwmc2 != ""){
//            wrapper.or().eq("dwmc",dwmc2);
//        };
//        if(bgateway_name2 != ""){
//            wrapper.or().eq("b_gateway_name",bgateway_name2);
//        };
//        if(collector_name2 != ""){
//            wrapper.or().eq("collector_name",collector_name2);
//        };
//        if(dwmc3 != ""){
//            wrapper.or().eq("dwmc",dwmc3);
//        };
//        if(bgateway_name3 != ""){
//            wrapper.or().eq("b_gateway_name",bgateway_name3);
//        };
//        if(collector_name3 != ""){
//            wrapper.or().eq("collector_name",collector_name3);
//        };
//        if(dwmc4 != ""){
//            wrapper.or().eq("dwmc",dwmc4);
//        };
//        if(bgateway_name4 != ""){
//            wrapper.or().eq("b_gateway_name",bgateway_name4);
//        };
//        if(collector_name4 != ""){
//            wrapper.or().eq("collector_name",collector_name4);
//        };

        if(dwmc != "") {
            wrapper.and(i -> i.eq("dwmc", dwmc)
                    .or().eq("dwmc", dwmc1)
                    .or().eq("dwmc", dwmc2)
                    .or().eq("dwmc", dwmc3)
                    .or().eq("dwmc", dwmc4));
        }
        if(bgateway_name != "") {
            wrapper.and(i -> i.eq("b_gateway_name", bgateway_name)
                    .or().eq("b_gateway_name", bgateway_name1)
                    .or().eq("b_gateway_name", bgateway_name2)
                    .or().eq("b_gateway_name", bgateway_name3)
                    .or().eq("b_gateway_name", bgateway_name4));
        }
        if(collector_name != "") {
            wrapper.and(i -> i.eq("collector_name", collector_name)
                    .or().eq("collector_name", collector_name1)
                    .or().eq("collector_name", collector_name2)
                    .or().eq("collector_name", collector_name3)
                    .or().eq("collector_name", collector_name4));
        }
        IPage<BCollectionDO> paging = BCollectionService.getBaseMapper().selectPage(pager, wrapper);
        return PageUtil.build(paging);
    }

    @GetMapping("/search")
    public PageResponseVO<BCollectionDO> search(
            @RequestParam(name = "count", required = false, defaultValue = "10")
            @Min(value = 1, message = "{page.count.min}") Integer count,
            @RequestParam(name = "page", required = false, defaultValue = "0")
            @Min(value = 0, message = "{page.number.min}") Integer page,
            @RequestParam(name = "keyword") String keyword,
            @RequestParam(name = "StartTime") String StartTime,
            @RequestParam(name = "EndTime") String EndTime,
            @RequestParam(name = "dwmc") String dwmc,
            @RequestParam(name = "bgateway_name") String bgateway_name,
            @RequestParam(name = "collector_name") String collector_name,
            @RequestParam(name = "collector_power") String collector_power,
            @RequestParam(name = "tresult") String tresult,
            @RequestParam(name = "hresult") String hresult,
            //垃圾
            @RequestParam(name = "dwmc1") String dwmc1,
            @RequestParam(name = "bgateway_name1") String bgateway_name1,
            @RequestParam(name = "collector_name1") String collector_name1,
            @RequestParam(name = "dwmc2") String dwmc2,
            @RequestParam(name = "bgateway_name2") String bgateway_name2,
            @RequestParam(name = "collector_name2") String collector_name2,
            @RequestParam(name = "dwmc3") String dwmc3,
            @RequestParam(name = "bgateway_name3") String bgateway_name3,
            @RequestParam(name = "collector_name3") String collector_name3,
            @RequestParam(name = "dwmc4") String dwmc4,
            @RequestParam(name = "bgateway_name4") String bgateway_name4,
            @RequestParam(name = "collector_name4") String collector_name4
    ) {
        Page<BCollectionDO> pager = new Page<>(page, count);
        IPage<BCollectionDO> paging = BCollectionService.seachBCollection(pager,keyword,StartTime,EndTime,dwmc,bgateway_name,collector_name,collector_power,tresult,hresult,dwmc1,
                bgateway_name1,
                collector_name1,
                dwmc2,
                bgateway_name2,
                collector_name2,
                dwmc3,
                bgateway_name3,
                collector_name3,
                dwmc4,
                bgateway_name4,
                collector_name4);
        return PageUtil.build(paging);
    }
    
    @GetMapping("/{id}/detail")
    @LoginRequired
    public BCollectionDetailDO getDetail(@PathVariable(value = "id") @Positive Long id) {
        BCollectionDetailDO detail = this.BCollectionService.getDetail(id);
        return detail;
    }
}
